package byggemarked;

import Models.Item;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
//import java.util.Scanner;

/**
 * @author Morten
 */
public abstract class Console
{
	//private static Scanner inputReader = new Scanner(System.in);
	private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	/**
	 * Waits for the user to enter an integer
	 * @param minimum The minimum, a user can enter (usually 0)
	 * @param maximum The maximum, a user can enter
	 * @return The choice
	 */
	public static int readChoice(int minimum, int maximum)
	{
		int choice = 0;
		//choice = inputReader.nextInt();
		choice = Console.readInt();
		choice = (choice == 99)?0:choice;
		if(choice >= minimum && choice < maximum)
		{
			return choice;
		}
		return choice;
	}

	/**
	 * Waits for the user to enter a string
	 * @param allowEmpty Is it allowed not to enter anything?
	 * @return The string entered
	 */
	public static String readString(boolean allowEmpty)
	{
		String value = null;
		boolean hasValidValue = false;
		while(!hasValidValue)
		{
			try
			{
				//value = inputReader.nextLine();
				value = br.readLine();
			}
			catch (Exception e)
			{
				value = "";
			}
			if(value.equals(""))
			{
				if(allowEmpty)
				{
					hasValidValue = true;
				}
				else
				{
					hasValidValue = false;
					print("Det indtastede, er ikke gyldigt. Prøv igen.");
				}
			}
			else
			{
				hasValidValue = true;
			}
		}
		return value;
	}

	public static String readString()
	{
		return readString(false);
	}

	/**
	 * Waits for the user to enter a integer
	 * @param allowEmpty Is it allowed not to enter anything?
	 * @return The integer entered
	 */
	public static int readInt(boolean allowEmpty)
	{
		int value = 0;
		boolean hasValidValue = false;
		while(!hasValidValue)
		{
			String valueString = Console.readString(allowEmpty);
			if(valueString.equals(""))
			{
				if(allowEmpty == true)
				{
					value = 99;
					hasValidValue = true;
				}
			}
			else
			{
				try
				{
					value = Integer.parseInt(valueString);
					hasValidValue = true;
				}
				catch(NumberFormatException e)
				{ /*inputReader.nextLine();*/ }
			}
			if(!hasValidValue)
			{
				print("Det indtastede, er ikke gyldigt. Prøv igen.");
			}
		}
		return value;
	}

	public static int readInt()
	{
		return readInt(false);
	}

	/**
	 * Waits for the user to enter a double
	 * @param allowEmpty Is it allowed not to enter anything?
	 * @return The double entered
	 */
	public static double readDouble(boolean allowEmpty)
	{
		double value = 0d;
		boolean hasValidValue = false;
		while(!hasValidValue)
		{
			String valueString = Console.readString(allowEmpty);
			if(valueString.equals(""))
			{
				if(allowEmpty == true)
				{
					value = 0d;
					hasValidValue = true;
				}
			}
			else
			{
				try
				{
					value = Double.parseDouble(valueString);
					hasValidValue = true;
				}
				catch(NumberFormatException e)
				{ /*inputReader.nextLine();*/ }
			}
			if(!hasValidValue)
			{
				print("Det indtastede, er ikke gyldigt. Prøv igen.");
			}
		}
		return value;
	}

	public static double readDouble()
	{
		return readDouble(false);
	}

	/**
	 * Waits for the user to enter a date
	 * @param allowEmpty Is it allowed not to enter anything?
	 * @return The date entered
	 */
	public static Date readDate(boolean allowEmpty)
	{
		Date value = null;
		boolean hasValidValue = false;
		while(!hasValidValue)
		{
			String valueString = Console.readString(allowEmpty);
			if(valueString.equals(""))
			{
				if(allowEmpty == true)
				{
					value = null;
					hasValidValue = true;
				}
			}
			else
			{
				try
				{
					value = Tools.makeDateFromString(valueString);
					hasValidValue = true;
				}
				catch(NumberFormatException e)
				{ /*inputReader.nextLine();*/ }
			}
			if(!hasValidValue)
			{
				print("Det indtastede, er ikke gyldigt. Det skal være i formatet 29-05-2009. Prøv igen.");
			}
		}
		return value;
	}

	public static Date readDate()
	{
		return readDate(false);
	}

	/**
	 * Prints a menu with the specified options
	 * @param showTitles True if the titles at the top of the menu is going to be displayed
	 * @param title The title of the menu
	 * @param options The options in the menu
	 */
	public static void printMenu(boolean showTitles, String title, ArrayList<String> options)
	{
		if(showTitles)
		{
			print("\f ----- Vestbjerg Byggemarked -----");
			print("  ---  "+title+"  ---  ");
		}
		int i = 1;
		for (String option : options) {
			print(" {"+i+"} "+option);
			i++;
		}
		print(" {0} Luk/Tilbage");
		print("\n - Vælg et punkt!");
	}

	/**
	 * Prints some text to the console
	 * @param text The text to print
	 */
	public static void print(String text)
	{
		System.out.println(text);
	}

	/**
	 * Makes a list of the items in the array
	 * @param items The items
	 * @return A string of the items, seperated by a comma
	 */
	public static void printList(ArrayList<Item> items)
    {
        for (Item item : items)
        {
			print(item.getName());
        }
    }
}
